home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 June / PersonalComputerWorld-June2009-CoverdiscCD.iso / Software / Freeware / Firebug 1.3.3 / firebug-1.3.3-fx.xpi / components / firebug-trace-service.js < prev   
Encoding:
Text File  |  2009-02-19  |  10.0 KB  |  322 lines

  1. /* See license.txt for terms of usage */
  2.  
  3. // ************************************************************************************************
  4. // Constants
  5.  
  6. const CLASS_ID = Components.ID("{D2AC51BC-1622-4d4d-85CB-F8E8B5805CB9}");
  7. const CLASS_NAME = "Firebug Trace Console Service";
  8. const CONTRACT_ID = "@joehewitt.com/firebug-trace-service;1";
  9. const EXTENSIONS = "extensions";
  10. const DBG_ = "DBG_";
  11.  
  12. const Cc = Components.classes;
  13. const Ci = Components.interfaces;
  14. const Cr = Components.results;
  15.  
  16. const PrefService = Cc["@mozilla.org/preferences-service;1"];
  17. const prefs = PrefService.getService(Ci.nsIPrefBranch2);
  18. const prefService = PrefService.getService(Ci.nsIPrefService);
  19. const consoleService = Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService);
  20.  
  21. try {                                                                                                                                                             /*@explore*/
  22. const appShellService = Components.classes["@mozilla.org/appshell/appShellService;1"].getService(Components.interfaces.nsIAppShellService);                       /*@explore*/
  23. } catch (e) {}                                                                                                                                                    /*@explore*/
  24.  
  25. // ************************************************************************************************
  26. // Service implementation
  27.  
  28. //var win = appShellService.hiddenDOMWindow;
  29. const win = false;
  30.  
  31. function TraceConsoleService()  // singleton
  32. {
  33.     this.observers = [];
  34.     this.optionMaps = {};
  35.     
  36.     // Listen for preferences changes. Trace Options can be changed at run time.
  37.     prefs.addObserver("extensions", this, false);
  38.     
  39.     this.wrappedJSObject = this; // why not
  40. }
  41.  
  42. TraceConsoleService.prototype = 
  43. {
  44.     getTracer: function(prefDomain)  
  45.     {
  46.         if (!this.optionMaps[prefDomain])
  47.             this.optionMaps[prefDomain] = this.createManagedOptionMap(prefDomain);
  48.         if (win)
  49.             win.dump("TraceConsoleService.getTracer, prefDomain: "+prefDomain+"\n");
  50.         return this.optionMaps[prefDomain];
  51.     },
  52.  
  53.     createManagedOptionMap: function(prefDomain)
  54.     {
  55.         var optionMap = new TraceBase(prefDomain); 
  56.          
  57.         var branch = prefService.getBranch ( prefDomain );
  58.         var arrayDesc = {};
  59.         var children = branch.getChildList("", arrayDesc);
  60.         for (var i = 0; i < children.length; i++)
  61.         {
  62.             var p = children[i];
  63.             var m = p.indexOf("DBG_");   
  64.             if (m != -1)
  65.             {
  66.                 var optionName = p.substr(1); // drop leading .
  67.                 optionMap[optionName] = this.getPref(prefDomain+p);
  68.                 if (win)
  69.                     win.dump("TraceConsoleService.createManagedOptionMap "+optionName+"="+optionMap[optionName]+"\n");
  70.             }
  71.         }
  72.         
  73.         return optionMap;
  74.     },
  75.     
  76.     /* nsIObserve */
  77.     observe: function(subject, topic, data)
  78.     {
  79.         if (data.substr(0,EXTENSIONS.length) == EXTENSIONS)
  80.         {
  81.             for (var prefDomain in gTraceService.optionMaps)
  82.             {
  83.                 if (data.substr(0, prefDomain.length) == prefDomain)
  84.                 {
  85.                     var optionName = data.substr(prefDomain.length+1); // skip dot
  86.                     if (optionName.substr(0, DBG_.length) == DBG_)
  87.                         gTraceService.optionMaps[prefDomain][optionName] = this.getPref(data);
  88.                     if (win)
  89.                         win.dump("TraceConsoleService.observe, prefDomain: "+prefDomain+" optionName "+optionName+"\n");
  90.                 }
  91.             }
  92.         }
  93.     },
  94.  
  95.     getPref: function(prefName)
  96.     {
  97.         var type = prefs.getPrefType(prefName);
  98.         if (type == Ci.nsIPrefBranch.PREF_STRING)
  99.             return prefs.getCharPref(prefName);
  100.         else if (type == Ci.nsIPrefBranch.PREF_INT)
  101.             return prefs.getIntPref(prefName);
  102.         else if (type == Ci.nsIPrefBranch.PREF_BOOL)
  103.             return prefs.getBoolPref(prefName);
  104.     },
  105.  
  106.     // Prepare trace-object and dispatch to all observers.
  107.     dispatch: function(messageType, message, obj)
  108.     {
  109.         // Translate string object.
  110.         if (typeof(obj) == "string") {
  111.             var string = Cc["@mozilla.org/supports-cstring;1"].createInstance(Ci.nsISupportsCString);
  112.             string.data = obj;
  113.             obj = string;
  114.         }
  115.  
  116.         // Create wrapper with message type info.
  117.         var messageInfo = {
  118.             obj: obj, 
  119.             type: messageType
  120.         };
  121.         if (win)
  122.             win.dump("TraceConsoleService.dispatch, prefDomain: "+messageType+"\n");
  123.         // Pass JS object properly through XPConnect.
  124.         var wrappedSubject = {wrappedJSObject: messageInfo};
  125.         gTraceService.notifyObservers(wrappedSubject, "firebug-trace-on-message", message);
  126.     },
  127.  
  128.     /* nsIObserverService */
  129.     addObserver: function(observer, topic, weak)
  130.     {
  131.         if (topic != "firebug-trace-on-message")
  132.             throw Cr.NS_ERROR_INVALID_ARG;
  133.     
  134.         this.observers.push(observer);
  135.     },
  136.  
  137.     removeObserver: function(observer, topic)
  138.     {
  139.         if (topic != "firebug-trace-on-message")
  140.             throw Cr.NS_ERROR_INVALID_ARG;
  141.  
  142.         for (var i=0; i < this.observers.length; i++) {
  143.             if (this.observers[i] == observer) {
  144.                 this.observers.splice(i, 1);
  145.                 break;
  146.             }
  147.         }
  148.     },
  149.  
  150.     notifyObservers: function(subject, topic, someData)
  151.     {
  152.         try
  153.         {
  154.             if (this.observers.length > 0)
  155.             {
  156.                 for (var i=0; i<this.observers.length; i++)
  157.                     this.observers[i].observe(subject, topic, someData);
  158.             }
  159.             else if (appShellService)                                                     /*@explore*/
  160.             {                                                                             /*@explore*/
  161.                 var hiddenWindow = appShellService.hiddenDOMWindow;                       /*@explore*/
  162.                 var unwrapped = subject.wrappedJSObject;                                  /*@explore*/
  163.                 var objPart = unwrapped.obj ? (" obj: "+unwrapped.obj) : "";              /*@explore*/
  164.                 hiddenWindow.dump("FTS: "+someData+objPart+"\n");                         /*@explore*/
  165.             }                                                                             /*@explore*/
  166.         }
  167.         catch (err)
  168.         {
  169.             // If it's not possible to distribute the log through registered observers,
  170.             // use Firefox ErrorConsole. Ulimately the trace-console listens for it
  171.             // too and so, will display that.
  172.             var scriptError = Cc["@mozilla.org/scripterror;1"].createInstance(Ci.nsIScriptError);
  173.             scriptError.init("[JavaScript Error: Failed to notify firebug-trace observers!] " +
  174.                 err.toString(), err.sourceName,
  175.                 err.sourceLine, err.lineNumber, err.columnNumber, err.flags, err.category);
  176.             consoleService.logMessage(scriptError);
  177.         }
  178.     },
  179.  
  180.     enumerateObservers: function(topic)
  181.     {
  182.         return null;
  183.     },
  184.  
  185.     /* nsISupports */
  186.     QueryInterface: function(iid) 
  187.     {
  188.         if (iid.equals(Ci.nsISupports) ||
  189.             iid.equals(Ci.nsIObserverService))
  190.              return this;
  191.         
  192.         throw Cr.NS_ERROR_NO_INTERFACE;
  193.     }
  194. };
  195.  
  196. // ************************************************************************************************
  197. // Public TraceService API
  198.  
  199. // Prevent tracing from code that performs tracing.
  200. var noTrace = false;
  201.  
  202. var TraceAPI = {    
  203.     dump: function(messageType, message, obj) {
  204.         if (noTrace)
  205.             return;
  206.  
  207.         noTrace = true;
  208.         gTraceService.dispatch(messageType, message, obj);
  209.         noTrace = false;
  210.     },
  211.  
  212.     sysout: function(message, obj) {
  213.         this.dump(null, message, obj);
  214.     },
  215.  
  216.     // OBSOLETE
  217.     dumpProperties: function(message, obj) {
  218.         this.sysout(message, obj);
  219.     },
  220.  
  221.     dumpStack: function(message) {
  222.         this.sysout(message);
  223.     },
  224.  
  225.     dumpEvent: function(message, eventObj) {
  226.         this.sysout(message, eventObj);
  227.     },
  228.  
  229.     dumpInterfaces: function(message, eventObj) {
  230.         this.sysout(message, eventObj);
  231.     }
  232. };
  233.  
  234. var TraceBase = function(prefDomain) {
  235.     this.prefDomain = prefDomain;
  236.     this.sysout = function(message, obj) {
  237.         TraceAPI.dump(this.prefDomain, message, obj);
  238.     }
  239. }
  240. //Derive all properties from TraceAPI
  241. for (var p in TraceAPI)
  242.     TraceBase.prototype[p] = TraceAPI[p];
  243.  
  244.  
  245.  
  246.  
  247.  
  248. // ************************************************************************************************
  249. // Service factory
  250.  
  251. var gTraceService = null;
  252. var TraceConsoleServiceFactory = 
  253. {
  254.     createInstance: function (outer, iid)
  255.     {
  256.         if (outer != null)
  257.             throw Cr.NS_ERROR_NO_AGGREGATION;
  258.  
  259.         if (iid.equals(Ci.nsISupports) ||
  260.             iid.equals(Ci.nsIObserverService))
  261.         {
  262.             if (!gTraceService)
  263.                 gTraceService = new TraceConsoleService();
  264.             return gTraceService.QueryInterface(iid);
  265.         }
  266.         
  267.         throw Cr.NS_ERROR_NO_INTERFACE;
  268.     },
  269.     
  270.     QueryInterface: function(iid) 
  271.     {
  272.         if (iid.equals(Ci.nsISupports) ||
  273.             iid.equals(Ci.nsISupportsWeakReference) ||
  274.             iid.equals(Ci.nsIFactory))
  275.             return this;
  276.             
  277.         throw Cr.NS_ERROR_NO_INTERFACE;
  278.     }
  279. };
  280.  
  281. // ************************************************************************************************
  282. // Module implementation
  283.  
  284. var TraceConsoleServiceModule =
  285. {
  286.     registerSelf: function (compMgr, fileSpec, location, type)
  287.     {
  288.         compMgr = compMgr.QueryInterface(Ci.nsIComponentRegistrar);
  289.         compMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME, 
  290.             CONTRACT_ID, fileSpec, location, type);
  291.     },
  292.  
  293.     unregisterSelf: function(compMgr, fileSpec, location)
  294.     {
  295.         compMgr = compMgr.QueryInterface(Ci.nsIComponentRegistrar);
  296.         compMgr.unregisterFactoryLocation(CLASS_ID, location);
  297.     },
  298.  
  299.     getClassObject: function (compMgr, cid, iid)
  300.     {
  301.         if (!iid.equals(Ci.nsIFactory))
  302.             throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  303.  
  304.         if (cid.equals(CLASS_ID))
  305.             return TraceConsoleServiceFactory;
  306.  
  307.         throw Cr.NS_ERROR_NO_INTERFACE;
  308.     },
  309.  
  310.     canUnload: function(compMgr)
  311.     {
  312.         return true;
  313.     }
  314. };
  315.  
  316. // ************************************************************************************************
  317.  
  318. function NSGetModule(compMgr, fileSpec)
  319. {
  320.     return TraceConsoleServiceModule;
  321. }
  322.